package cn.cdzdjd.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.codec.Base64Decoder;

public class UrlUtil {
	public static Map<String, String> convertUrlParamsToMap(URL url) {
		// 使用URI类解析URL中的查询参数
		String q = url.getQuery();
		Map<String, String> paramMap = new HashMap<>();
		if (q != null) {
			String query = q.replaceAll("&amp;", "&");
			// 将查询参数字符串拆分为键值对
			String[] params = query.split("&");
			for (String param : params) {
				String[] keyValue = param.split("=", 2);
				if (keyValue.length > 1) {
					paramMap.put(keyValue[0], keyValue[1]);
				} else if (keyValue.length == 1 && param.indexOf('=') == param.length() - 1) {
					// 处理没有值的键，如 "key="
					paramMap.put(keyValue[0], "");
				}
			}
		}
		return paramMap;
	}

	public static String decodeURL(String url) {

		return url.replaceAll("&amp;", "&");
	}

	public static String getPath(String url) {
		try {
			URL u = new URL(url);
			String path = u.getPath();
			return path;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public static String getPath(URL url) {
		String path = url.getPath();
		return path;
	}

	/**
	 * http转https开头
	 * 
	 * @param url
	 * @return
	 */
	public static String setHttps(String url) {
		if (url.startsWith("https")) {
			return url;
		} else {
			return url.replaceAll("http", "https");
		}
	}

	public static String decodeVideoUrl(String base64Code) {
		String[] hexMap = getHex(base64Code);
		List<Integer> decList = getDec(hexMap[0]);
		String d = sub_str(hexMap[1], decList.get(0));
		List<Integer> p = getPos(d, decList.get(1).toString());
		String result = sub_str(d, p);
		return "https:" + Base64Decoder.decodeStr(result);
	}

	private static String[] getHex(String code) {
		String hex_1 = StringUtils.substring(code, 0, 4);
		String str_1 = StringUtils.substring(code, 4);
		return new String[] { StringUtils.reverse(hex_1), str_1 };
	}

	private static List<Integer> getDec(String hex) {
		int b = hex2ten(hex);
		int pre = Integer.valueOf(StringUtils.substring(String.valueOf(b), 0, 2));
		int tail = Integer.valueOf(StringUtils.substring(String.valueOf(b), 2));
		List<Integer> list = new ArrayList<>();
		list.add(pre);
		list.add(tail);
		return list;
	}

	private static int hex2ten(String hex) {
		return Integer.parseInt(hex, 16);
	}

	private static String sub_str(String a, List<Integer> b) {
		int k = b.get(0);
		int j = b.get(1);
		String c = StringUtils.substring(a, 0, k);
		String temp = StringUtils.substring(a, k + j);
		return c + temp;
	}

	private static String sub_str(String a, Integer b) {
		int k = Integer.valueOf(b.toString().charAt(0) + "");
		int j = Integer.valueOf(b.toString().charAt(1) + "");

		String c = StringUtils.substring(a, 0, k);
		String temp = StringUtils.substring(a, k + j);
		return c + temp;
	}

	private static List<Integer> getPos(String a, String b) {
		int al = a.length();
		int b0 = Integer.valueOf(b.charAt(0) + "");
		int b1 = Integer.valueOf(b.charAt(1) + "");
		int s = al - b0 - b1;
		List<Integer> list = new ArrayList<>();
		list.add(s);
		list.add(b1);
		return list;

	}
}
